﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormApp1
{
    public partial class PreviewImage : Form
    {
        public PreviewImage()
        {
            InitializeComponent();


            this.Load += new System.EventHandler(this.PreviewImage_Load);
            //this.pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);

            this.pictureBox1.MouseMove += pictureBox1_MouseMove;
            this.pictureBox1.MouseUp += pictureBox1_MouseUp;
            this.pictureBox1.MouseDown += pictureBox1_MouseDown;

            this.panel2.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
            //this.panel2.MouseWheel += new MouseEventHandler(panel2_MouseWheel);

            SetCtrlDrag.SetDragImage(this.pictureBox1);

            this.button1.Visible = false;
            this.button2.Visible = false;
            this.button3.Visible = false;
        }

        private void PreviewImage_Load(object sender, EventArgs e)
        {
            //pictureBox1.Image = myBmp;
        }

        private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            this.pictureBox1.Focus();
        }
        //Image myBmp = Image.FromFile("e:\\下载\\tim22g.jpeg");
        int zoomStep = 50;
        private void panel2_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //if (e.Delta > 0)
            //{
            //    this.Text = "Mouse Wheeled Up " + e.Delta;
            //}
            //else
            //{
            //    this.Text = "Mouse Wheeled Down " + e.Delta;
            //}
            var myBmp = pictureBox1.Image;

            int x = e.Location.X;
            int y = e.Location.Y;
            int ow = pictureBox1.Width;
            int oh = pictureBox1.Height;
            int VX, VY;     //因缩放产生的位移矢量
            if (e.Delta > 0)    //放大
            {
                //第①步
                pictureBox1.Width += zoomStep;
                pictureBox1.Height += zoomStep;
                ////第②步
                //PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
                //    BindingFlags.NonPublic);
                //Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);
                ////第③步
                //pictureBox1.Width = rect.Width;
                //pictureBox1.Height = rect.Height;
            }
            if (e.Delta < 0)    //缩小
            {
                //防止一直缩成负值
                if (pictureBox1.Width < myBmp.Width / 10)
                    return;

                pictureBox1.Width -= zoomStep;
                pictureBox1.Height -= zoomStep;
                PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
                    BindingFlags.NonPublic);
                Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);
                pictureBox1.Width = rect.Width;
                pictureBox1.Height = rect.Height;
            }
            ////第④步，求因缩放产生的位移，进行补偿，实现锚点缩放的效果
            //VX = (int)((double)x * (ow - pictureBox1.Width) / ow);
            //VY = (int)((double)y * (oh - pictureBox1.Height) / oh);
            //pictureBox1.Location = new Point(pictureBox1.Location.X + VX, pictureBox1.Location.Y + VY);

        }
        //实现锚点缩放(以鼠标所指位置为中心缩放)；
        //步骤：
        //①先改picturebox长宽，长宽改变量一样；
        //②获取缩放后picturebox中实际显示图像的长宽，这里长宽是不一样的；
        //③将picturebox的长宽设置为显示图像的长宽；
        //④补偿picturebox因缩放产生的位移，实现锚点缩放。
        // 注释：为啥要②③步？由于zoom模式的机制，把picturebox背景设为黑就知道为啥了。
        //这里需要获取zoom模式下picturebox所显示图像的大小信息，添加 using System.Reflection；
        //pictureBox1_MouseWheel事件没找到。。。手动添加，别忘在Form1.Designer.cs的“Windows 窗体设计器生成的代码”里加入：  
        //this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseWheel)。
        private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            var myBmp = pictureBox1.Image;


            int x = e.Location.X;
            int y = e.Location.Y;
            int ow = pictureBox1.Width;
            int oh = pictureBox1.Height;
            int VX, VY;  //因缩放产生的位移矢量
            if (e.Delta > 0) //放大
            {
                //第①步
                pictureBox1.Width += zoomStep;
                pictureBox1.Height += zoomStep;
                //第②步
                PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
                 BindingFlags.NonPublic);
                Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);
                //第③步
                pictureBox1.Width = rect.Width;
                pictureBox1.Height = rect.Height;
            }
            if (e.Delta < 0) //缩小
            {
                //防止一直缩成负值
                if (pictureBox1.Width < myBmp.Width / 10)
                    return;

                pictureBox1.Width -= zoomStep;
                pictureBox1.Height -= zoomStep;
                PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
                 BindingFlags.NonPublic);
                Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);
                pictureBox1.Width = rect.Width;
                pictureBox1.Height = rect.Height;
            }
            //第④步，求因缩放产生的位移，进行补偿，实现锚点缩放的效果
            VX = (int)((double)x * (ow - pictureBox1.Width) / ow);
            VY = (int)((double)y * (oh - pictureBox1.Height) / oh);
            pictureBox1.Location = new Point(pictureBox1.Location.X + VX, pictureBox1.Location.Y + VY);
        }


        Point mouseDownPoint = new Point(0, 0);
        bool isMove = false;
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseDownPoint.X = Cursor.Position.X; //记录鼠标左键按下时位置
                mouseDownPoint.Y = Cursor.Position.Y;
                isMove = true;
                pictureBox1.Focus(); //鼠标滚轮事件(缩放时)需要picturebox有焦点
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMove = false;
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            pictureBox1.Focus(); //鼠标在picturebox上时才有焦点，此时可以缩放
            if (isMove)
            {
                int x, y;   //新的pictureBox1.Location(x,y)
                int moveX, moveY; //X方向，Y方向移动大小。
                moveX = Cursor.Position.X - mouseDownPoint.X;
                moveY = Cursor.Position.Y - mouseDownPoint.Y;
                x = pictureBox1.Location.X + moveX;
                y = pictureBox1.Location.Y + moveY;
                pictureBox1.Location = new Point(x, y);
                mouseDownPoint.X = Cursor.Position.X;
                mouseDownPoint.Y = Cursor.Position.Y;
            }
        }

        //private void panel2_MouseDown(object sender, MouseEventArgs e)
        //{
        //    if (e.Button == MouseButtons.Left)
        //    {
        //        mouseDownPoint.X = Cursor.Position.X; //记录鼠标左键按下时位置
        //        mouseDownPoint.Y = Cursor.Position.Y;
        //        isMove = true;
        //    }
        //}

        //private void panel2_MouseUp(object sender, MouseEventArgs e)
        //{
        //    if (e.Button == MouseButtons.Left)
        //    {
        //        isMove = false;
        //    }
        //}

        //private void panel2_MouseMove(object sender, MouseEventArgs e)
        //{
        //    panel2.Focus(); //鼠标不在picturebox上时焦点给别的控件，此时无法缩放   
        //    if (isMove)
        //    {
        //        int x, y;   //新的pictureBox1.Location(x,y)
        //        int moveX, moveY; //X方向，Y方向移动大小。
        //        moveX = Cursor.Position.X - mouseDownPoint.X;
        //        moveY = Cursor.Position.Y - mouseDownPoint.Y;
        //        x = pictureBox1.Location.X + moveX;
        //        y = pictureBox1.Location.Y + moveY;
        //        pictureBox1.Location = new Point(x, y);
        //        mouseDownPoint.X = Cursor.Position.X;
        //        mouseDownPoint.Y = Cursor.Position.Y;
        //    }
        //}
    }
    public class SetCtrlDrag
    {
        public static void SetDragImage(Control ctrl)
        {
            if (ctrl is PictureBox)
            {
                var pt = ctrl as PictureBox;
                pt.AllowDrop = true;
                pt.DragEnter += (sender, e) =>
                {
                    e.Effect = DragDropEffects.Link;//拖动时的图标
                };

                pt.DragDrop += (sender, e) =>
                {
                    //((TextBox)sender).Text = "文件路径：" + ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
                    //
                    //System.Diagnostics.Debug.Print(
                    //    ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString()
                    //    );
                    pt.Image = Image.FromFile(
                        ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString()
                        );
                };
            }
        }
    }

}
